/******************************************************************************
* file  : console.c
* creat : apleilx
* data  : 2021/01/20
*******************************************************************************/
#include "os_obj.h"
#include "systask.h"
#include "com_obj.h"
#include "dataproc.h"
#include "common.h"
#include "simp_txt_analysis.h"
#include "password.h"
#include "iohandle.h"
#include "str_ext.h"

uint32_t cmd_back_dat[16];
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num);

#define CMD_ATTR_DATE_R (1)
#define CMD_ATTR_DATE_S (CMD_ATTR_DATE_R + 1)
#define CMD_ATTR_DATE (CMD_ATTR_DATE_S + 1)
#define CMD_ATTR_LOG (CMD_ATTR_DATE + 1)
#define CMD_ATTR_CORE_MARK (CMD_ATTR_LOG + 1)
#define CMD_ATTR_READ (CMD_ATTR_CORE_MARK + 1)
#define CMD_ATTR_CLR (CMD_ATTR_READ + 1)
#define CMD_ATTR_SDFAT (CMD_ATTR_CLR + 1)
#define CMD_ATTR_USBFAT (CMD_ATTR_SDFAT + 1)
#define CMD_ATTR_TASK (CMD_ATTR_USBFAT + 1)
#define CMD_ATTR_MUSED (CMD_ATTR_TASK + 1)
#define CMD_ATTR_SIZE (CMD_ATTR_MUSED + 1)
#define CMD_ATTR_END 127

#if CMD_ATTR_SIZE != 12
#error cfg cmd define error "at usb_file_cfg.c"
#endif

/*-----------------------------------------------------------------------------*
* bit 15    : not display in info.txt
* bit 14    : not display in any file
* bit 8-11  : 0 无需权限， 1-管理权限， 2-工厂权限
*-----------------------------------------------------------------------------*/
const txt_cmd_cfg_type cmd_list[] =
	{
		{0x0000 | CMD_ATTR_DATE_R, "DATE.R", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback},
		{0x0000 | CMD_ATTR_DATE_S, "DATE.S", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback},
		{0x0000 | CMD_ATTR_DATE, "DATE", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback},
		{0x0000 | CMD_ATTR_LOG, "LOG", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
		{0x0000 | CMD_ATTR_CORE_MARK, "COREMARK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},

		{0x8100 | CMD_ATTR_CLR, "CLR", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
		{0x8100 | CMD_ATTR_READ, "READ", TXT_CMD_TYPE_LONG, cmd_back_dat, 4, cmd_callback},
		{0x8100 | CMD_ATTR_SDFAT, "SDFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback},
		{0x8100 | CMD_ATTR_USBFAT, "USBFAT", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback},
		{0x8100 | CMD_ATTR_TASK, "OS-TASK", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
		{0x8100 | CMD_ATTR_MUSED, "MUSED", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback},
		{CMD_ATTR_END, "END", TXT_CMD_TYPE_END, cmd_back_dat, 1, cmd_callback},
};

/*******************************************************************************
* @brief  cmd_callback.
* \param[in] none
* \retval: none
*******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num)
{
	char *i8p;
	int16_t *i16p;
	int32_t *i32p;

	if (cmd_site > sizeof(cmd_list) / sizeof(txt_cmd_cfg_type))
		return;

	i8p = cmd_list[cmd_site].des;
	i16p = cmd_list[cmd_site].des;
	i32p = cmd_list[cmd_site].des;

	switch (cmd_list[cmd_site].attr & 0xFF)
	{
	case CMD_ATTR_DATE_R:
		break;
	case CMD_ATTR_DATE_S:
		break;
	case CMD_ATTR_DATE:
	{
		sig_time_type tm;

		tm.time.year = i16p[0];
		tm.time.mon = i16p[1];
		tm.time.day = i16p[2];
		tm.time.hour = i16p[3];
		tm.time.min = i16p[4];
		tm.time.sec2t = i16p[5] >> 1;

		dbg_print("tm = %2d %2d %2d %2d %2d %2d => %d\n",
				  i16p[0], i16p[1], i16p[2], i16p[3], i16p[4], i16p[5],
				  sig_time_to_min(tm));

		sys_st.log.tm_set.val = tm.val;
		sys_st.log.tm_search_req = 1;
		break;
	}
	case CMD_ATTR_LOG:
	
		break;
	
	case CMD_ATTR_CORE_MARK:
	
		break;
	
	case CMD_ATTR_CLR:
	
		break;
	
	case CMD_ATTR_READ:
	
		sys_st.log.read_amount = i32p[0];
		break;
	
	case CMD_ATTR_SDFAT:
	
		break;
	
	case CMD_ATTR_USBFAT:
	
		sys_st.cmd.copy_usb_to_afat = 1;
		break;
	
	case CMD_ATTR_TASK:
	
		os_task_info();
		break;
	
	case CMD_ATTR_MUSED:
	
		dbg_print("system stack used : %.1f%%\n", (float)sys_st.info.stack_used / 10);
		break;
	
	default:
	
		break;
	
	}
}
